package cn.dcw.pethome.order.service.impl;

import cn.dcw.pethome.basic.service.impl.BaseServiceImpl;
import cn.dcw.pethome.basic.util.CodeGenerateUtils;
import cn.dcw.pethome.order.domain.AdoptOrder;
import cn.dcw.pethome.order.domain.OrderAddress;
import cn.dcw.pethome.order.mapper.AdoptOrderMapper;
import cn.dcw.pethome.order.mapper.OrderAddressMapper;
import cn.dcw.pethome.order.service.IAdoptOrderService;
import cn.dcw.pethome.pet.domain.Pet;
import cn.dcw.pethome.pet.mapper.PetMapper;
import cn.dcw.pethome.user.domain.LoginInfo;
import cn.dcw.pethome.user.domain.User;
import cn.dcw.pethome.user.domain.UserAddress;
import cn.dcw.pethome.user.mapper.UserAddressMapper;
import cn.dcw.pethome.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
public class AdoptOrderServiceImpl extends BaseServiceImpl<AdoptOrder> implements IAdoptOrderService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AdoptOrderMapper adoptOrderMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    /**
     * 领养订单
     * @param params
     * @param loginInfo
     * @return
     */
    @Override
    @Transactional
    public String submit(Map<String, Object> params, LoginInfo loginInfo) {

        /*
          service_method:'0',服务方式
                            address_id: '1',  地址id
                            pay_method:'2',支付方式
                            pet_id:null,下单宠物的id
         */
        Long petId = Long.valueOf(params.get("pet_id").toString()); //宠物id
        Long addressId = Long.valueOf(params.get("address_id").toString());//用户地址id
        Integer serviceMethod = Integer.valueOf(params.get("service_method").toString()); //配送方式
        Integer payMethod = Integer.valueOf(params.get("pay_method").toString()); //支付方
        //客户点击领养后，将pet表中的static修改为0，下架  领养用户id进行关联
        Pet pet = petMapper.getPetid(petId);
        pet.setState(0);//下架
        Long loginInfoId = loginInfo.getId();
        User user = userMapper.findOneByloginInfoId(loginInfoId);
        pet.setUser_id(user.getId());
        pet.setOffsaletime(new Date());//设置下架时间
        petMapper.update(pet);

        //3 创建领养订单
        //3.1 创建订单本身
        AdoptOrder adoptOrder = initAdoptOrder(user, pet);

        //@TODO 配送方式添加
        //3.2 创建订单地址
        UserAddress userAddress = userAddressMapper.findOne(addressId);
        adoptOrder.setAddress_id(userAddress.getId());
        adoptOrderMapper.add(adoptOrder);
        OrderAddress orderAddress = userAddress2OrderAddress(userAddress, adoptOrder);
        //userAddress的id对应adoptOrder.setAddress_id关联

        orderAddressMapper.add(orderAddress);


        return null;
    }

    public OrderAddress userAddress2OrderAddress(UserAddress userAddress,AdoptOrder adoptOrder){
        OrderAddress orderAddress = new OrderAddress();
        //对象复制
        BeanUtils.copyProperties(userAddress,orderAddress);
        orderAddress.setId(null);//id不需要，自增长
        orderAddress.setOrder_id(adoptOrder.getId());
        orderAddress.setOrderSn(adoptOrder.getOrderSn());
        return orderAddress;
    }
    //通过user和pet生成支付订单
    public AdoptOrder initAdoptOrder(User user,Pet pet){
        AdoptOrder adoptOrder = new AdoptOrder();
        adoptOrder.setDigest("[摘要]领养"+pet.getName()+"的订单！");
        adoptOrder.setState(0); //待支付
        adoptOrder.setPrice(pet.getSaleprice()); //待支付
        //CodeGenerateUtils工具类 获取随机商品编码
        adoptOrder.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        //lastPayTime最后支付时间  当前时间+15分钟
        Date lastPayTime = new Date(System.currentTimeMillis()+15*60*1000);
        adoptOrder.setLastPayTime(lastPayTime);
        adoptOrder.setPet_id(pet.getId());
        adoptOrder.setUser_id(user.getId());
        adoptOrder.setShop_id(pet.getShop_id());
        return adoptOrder;
    }
}
